﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace LIB
{
    public class Converter
    {
        public static string toCurency(string money)
        {
            string m = money.ToString();
            string convert = "";
            int count = 0;

            for (int i = m.Length - 1; i >= 0; i--)
            {
                convert = String.Concat(convert, m[i].ToString());
                count++;
                if (count == 3 && i != 0)
                {
                    convert = String.Concat(convert, ".");
                    count = 0;
                }
            }

            return Converter.revert(convert);
        }

        public static string revert(string s)
        {
            string temp = "";
            for (int i = s.Length - 1; i >= 0; i--)
            {
                temp = String.Concat(temp, s[i].ToString());
            }
            return temp;
        }

        public static string Encode(string method, string input)
        {
            byte[] data;
            switch (method)
            {
                case "sha1":
                    data = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(input)); SHA1.Create();
                    break;
                case "sha256":
                    data = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
                    break;
                case "sha384":
                    data = SHA384.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
                    break;
                case "sha512":
                    data = SHA512.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
                    break;
                default:
                    data = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
                    break;
            }
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }

        public static string RandomString(int length)
        {
            string allowedLetterChars = "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
            string allowedNumberChars = "0123456789";
            char[] chars = new char[length];
            Random rd = new Random();
            bool useLetter = true;
            for (int i = 0; i < length; i++)
            {
                if (useLetter)
                {
                    chars[i] = allowedLetterChars[rd.Next(0, allowedLetterChars.Length)];
                    useLetter = false;
                }
                else
                {
                    chars[i] = allowedNumberChars[rd.Next(0, allowedNumberChars.Length)];
                    useLetter = true;
                }
            }
            return new string(chars);
        }
        private static readonly string[] VietnameseSigns = new string[]

    {

        "aAeEoOuUiIdDyY",

        "áàạảãâấầậẩẫăắằặẳẵ",

        "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ",

        "éèẹẻẽêếềệểễ",

        "ÉÈẸẺẼÊẾỀỆỂỄ",

        "óòọỏõôốồộổỗơớờợởỡ",

        "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ",

        "úùụủũưứừựửữ",

        "ÚÙỤỦŨƯỨỪỰỬỮ",

        "íìịỉĩ",

        "ÍÌỊỈĨ",

        "đ",

        "Đ",

        "ýỳỵỷỹ",

        "ÝỲỴỶỸ"

    };



        public static string RemoveSign4VietnameseString(string str)
        {

            //Tiến hành thay thế , lọc bỏ dấu cho chuỗi
          
            for (int i = 1; i < VietnameseSigns.Length; i++)
            {

                for (int j = 0; j < VietnameseSigns[i].Length; j++)

                    str = str.Replace(VietnameseSigns[i][j], VietnameseSigns[0][i - 1]);

            }

            return str;

        }
        public static string convertLink(string s)
        {
            if (s != null)
            {
                return RemoveSign4VietnameseString(s).ToLower().Replace(" ", "-");
            }
            return "";
        }

        public static string convertParam(string s)
        {
            string param = "";
            param = s.Replace("'", "");
            return param;
        }


        public static string trimString(string text) {

            return "";
        }
    }
}